package com.ymatou.iapi.optrade.service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.google.common.collect.Iterables;
import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.optrade.parameter.enums.BondedAreaEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingProductVo;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sharding.db.ext.model.IdcardBlackListPo;
import com.ymttest.database.sharding.db.ext.model.IdcardCostPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
 * 
 * 2W 限额服务
 *
 */
public class IdCardService {

//	//订单金额是否超过2K 
//	private static final BigDecimal PER_ORDER_LIMIT = new BigDecimal(2000);
//	//每年每个身份证消费上限2W元
//	private static final BigDecimal COST_LIMIT = new BigDecimal(20000);

	//在身份证限额2W黑名单用户
	private static String Idcard_Black_User_sit1 = "福诸";
	private static String Idcard_Black_User_IdNo_sit1 = "430523199712227418";

	//TODO uat 数据
	private static String Idcard_Black_User_uat = "";
	private static String Idcard_Black_User_IdNo_uat = "";

	private static String Idcard_Cost_User_sit1 = "高临";
	private static String Idcard_Cost_User_IdNo_sit1 = "520122199005060982";

	//TODO uat 数据
	private static String Idcard_Cost_User_uat = "";
	private static String Idcard_Cost_User_IdNo_uat = "";

	public static String Idcard_Black_User, Idcard_Cost_User,
			Idcard_Black_User_IdNo, Idcard_Cost_User_IdNo;

	static {

		if (EnvSetup.getEnv().equals("sit1")) {

			Idcard_Black_User = Idcard_Black_User_sit1;
			Idcard_Cost_User = Idcard_Cost_User_sit1;
			Idcard_Black_User_IdNo = Idcard_Black_User_IdNo_sit1;
			Idcard_Cost_User_IdNo = Idcard_Cost_User_IdNo_sit1;

		} else {
			Idcard_Black_User = Idcard_Black_User_uat;
			Idcard_Cost_User = Idcard_Cost_User_uat;
			Idcard_Black_User_IdNo = Idcard_Black_User_IdNo_uat;
			Idcard_Cost_User_IdNo = Idcard_Cost_User_IdNo_uat;
		}
	}

	/**
	 * 根据收件人,物流类型,是否预测电商件,商品价格 下单
	 * @param recvName
	 * @param logisticsType
	 * @param isEcommerceFlag
	 * @param productPrice
	 * @return
	 */
	public static TradingServiceResp placeOrder(String recvName,
			LogisticsTypeEnum logisticsType, boolean isEcommerceFlag,
			BigDecimal productPrice) {

		Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
		String sellerName = String.valueOf(EnvSetup.getData("autotest_sellerName1"));
		
		PlaceOrderReq placeorderBean = TradingCallService
				.buildStandPlaceOrderReq();
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		// 创建一个普通商品
		TradingProductVo product = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		
		OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
				product.getProductId(), product.getCatalogId());

		orderItemVo.setBondedArea(BondedAreaEnum.HangZhou);
		orderItemVo.setProductPrice(productPrice);
		orderItemVo.setOriginalPrice(productPrice);
		orderItemVo.setFreight(BigDecimal.ZERO);
		orderItemVo.setProductNum(1);
		
		orderVo.setLogisticsType(logisticsType);
		orderVo.setEcommerceFlag(isEcommerceFlag);
		orderVo.setOrderItems(Arrays.asList(orderItemVo));
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		
		placeorderBean.setOrderGroups(Arrays.asList(orderGroupVo));
		placeorderBean.setRecvName(recvName);
	
		return TradingCallService.callPlaceOrderService(placeorderBean);
	}
	/**
	 * updateIdcardCost
	 * @param userId
	 * @param recvName
	 * @param Amount
	 */
	public static void updateIdcardCost(long userId, String recvName,
			BigDecimal Amount) {

		String idcardNo = Idcard_Cost_User_IdNo;

		OrderWapper orderWapper = new OrderWapper();

		IdcardCostPo res = orderWapper.selectIdcardCostByIdCardNo(idcardNo);

		IdcardCostPo record = new IdcardCostPo();

		if (res == null) {

			record.setAmount(Amount);
			record.setIdcardNo(idcardNo);
			record.setYear(2018);

			orderWapper.idcardCostMapper.insertSelective(record);

		} else {

			res.setAmount(Amount);
			res.setYear(2018);

			orderWapper.updatIdCardCostByIdCardNo(res, idcardNo);

		}

	}

	/**
	 * 修改 身份证限额黑名单表(idcard_blacklist)
	 * @param userId
	 * @param recvName
	 * @param Amount
	 */
	public static void updateIdcardBlackList(long userId, String recvName) {

		String idcardNo = Idcard_Black_User_IdNo;

		OrderWapper orderWapper = new OrderWapper();

		IdcardBlackListPo res = orderWapper
				.selectIdcardBlackListByIdCardNo(idcardNo);

		IdcardBlackListPo record = new IdcardBlackListPo();

		if (res == null) {

			record.setIdcardNo(idcardNo);
			record.setDeleted(false);
			record.setYear(2018);

			orderWapper.idcardBlackListMapper.insertSelective(record);

		}
	}

	private static YmtIdCardInfo buildYmtIdCardInfo(int userId) throws Exception {

		YmtIdCardInfo record = new YmtIdCardInfo();
		String key = "1234567890123456";

		record.setCardId(TestDataManager.getRandomUUID().toString());
		record.setBuyerId(userId);
		record.setIDCard(CipherUtil.encryptAES(key, TestDataManager.getCardId(20, 10)));
		record.setHasPic(true);
		record.setIsDelete(0);
		record.setName(TestDataManager.getChineseName());
		record.setPhone(CipherUtil.encryptAES(key, TestDataManager.getPhoneNum()));
		record.setStatus(1);
		record.setCardType(1);
		record.setOperater("insert testdate by autoteam");
		record.setRightSide(CipherUtil.encryptAES(key, "http://file.iapi.ymatou.com/G01/M00/3A/76/rBBlD1ncd7eAKwtqAABQMz0rvxE036_5_3_o.jpg"));
		record.setReverseSide(CipherUtil.encryptAES(key, "http://file.iapi.ymatou.com/G01/M00/3A/76/rBBlD1ncd7eAKwtqAABQMz0rvxE036_5_3_o.jpg"));
		record.setCheckIdCardStatus(200);
		record.setUpdateTime(new Date());
		record.setUploadTime(new Date());
		record.setCreateTime(new Date());

		return record;
	}
	/**
	 * getIdCardByUserId
	 * @param userId
	 * @param status  （1. 通过审核，或未开始审核， -1. 审核不通过）
	 * @param hasPic 是否有图片
	 * @param isDelete	0 未删除，1 已删除
	 * @throws Exception 
	 */
	public static YmtIdCardInfo getIdCardByUserId(long userId, int status,
			boolean hasPic, int isDelete) throws Exception {

		YmtIdCardInfoWapper ymtIdCardInfoWapper = new YmtIdCardInfoWapper();

		List<YmtIdCardInfo> res = ymtIdCardInfoWapper.selectByUserId(userId,
				status, hasPic, isDelete);

		if (CollectionUtils.isEmpty(res)) {

			YmtIdCardInfo record = buildYmtIdCardInfo((int) userId);

			record.setStatus(status);
			record.setHasPic(hasPic);
			record.setIsDelete(isDelete);

			Logger.info(String.format(
					"手工直接插1条数据到 Ymt_IdCardInfo 身份信息表里 userId:%s,cardNum:%s ",
					userId, record.getIDCard()));

			ymtIdCardInfoWapper.insertIdCardInfo(record);

			return record;

		}

		return Iterables.getFirst(res, null);

	}
	public static void main(String... agrs) throws Exception {

		getIdCardByUserId(20238699, 1, true, 1);

	}

}
